﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Cashier
{
    public partial class items_form : Form
    {
        public delegate void SendItemEntry(ItemEntry data);
        public SendItemEntry Send { get; set; }
        Dictionary<int, ItemEntry> items;

        public items_form()
        {
            InitializeComponent();
            items = new Dictionary<int, ItemEntry>();
            using (DatabaseServices service = new DatabaseServices())
            {
                service.FillDataToGrid("Select * from item", gridView_Item);
                List<ItemEntry> _items = service.GetAllItems();
                foreach (ItemEntry _i in _items)
                {
                    if (!items.ContainsKey(_i.ID))
                    {
                        items.Add(_i.ID, _i);
                    }
                    else
                    {
                        items[_i.ID] = _i;
                    }
                }
            }
        }
        
        private void gridView_Item_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int selectedId = (int)gridView_Item.Rows[e.RowIndex].Cells[0].Value;
                ucItem1.Data = items[selectedId];
            }
            catch { }
        }
        
        private void gridView_Item_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {          
                int selectedId=(int)gridView_Item.SelectedRows[0].Cells[0].Value;
                ItemEntry iEntry = items[selectedId];
                Send(iEntry);
                this.Close();
                this.Dispose();
            }
        }

        private void ucItem1_Load(object sender, EventArgs e)
        {

        }

        private void btn_Item_Add_Click(object sender, EventArgs e)
        {
            //ucItem1.
        }
    }
}
